#include "drawchart.h"
#include "nightcharts.h"
//#include <QtGui/QStaticText>

DrawChart::DrawChart(QObject *parent) :
QObject(parent)
{
}

void DrawChart::drawLineChart(int xNum, QString title, QString line_name)
{
	QString id("white");
	QImage pixmap(700,600,QImage::Format_ARGB32_Premultiplied);
	pixmap.fill(QColor(id).rgba());

	// set gap
	qreal min = this->findMin() ;
	qreal max = this->findMax() ;
	qreal gap = max - min ;
	gap = gap > 50.0 ? gap : 50.0 ;

	// write the color name
	QPainter painter(&pixmap);
	painter.setRenderHint(QPainter::Antialiasing);


	QPen pen(QColor("#FF7E00"), 3, Qt::SolidLine, Qt::RoundCap, Qt::RoundJoin);
	QPen pen1(Qt::black, 2, Qt::SolidLine, Qt::SquareCap, Qt::MiterJoin);
	QPen pen2(Qt::black, 1, Qt::DashDotLine, Qt::SquareCap, Qt::MiterJoin);

	// draw rectangle for chart
	painter.setPen(pen1);
	painter.fillRect(50,50,600,500,QColor("#BFD9FF"));
	painter.drawRect(50,50,600,500);

	// draw short lines of x
	painter.setPen(pen2);
	painter.drawLine(100,550,100,540);
	painter.drawLine(150,550,150,540);
	painter.drawLine(200,550,200,540);
	painter.drawLine(250,550,250,540);
	painter.drawLine(300,550,300,540);
	painter.drawLine(350,550,350,540);
	painter.drawLine(400,550,400,540);
	painter.drawLine(450,550,450,540);
	painter.drawLine(500,550,500,540);
	painter.drawLine(550,550,550,540);
	painter.drawLine(600,550,600,540);
	painter.drawLine(650,550,650,540);

	// draw lines of y
	painter.drawLine(50,450,650,450);
	painter.drawLine(50,350,650,350);
	painter.drawLine(50,250,650,250);
	painter.drawLine(50,150,650,150);

	// description for red line
	//    QPen pen3(Qt::black, 1, Qt::SolidLine, Qt::RoundCap, Qt::RoundJoin);
	//    painter.setPen(pen);
	//    painter.drawLine(50,650,70,650);
	//    QFont font;
	//    font.setPixelSize(20);
	//    painter.setPen(pen3);
	//    painter.setFont(font);
	//    painter.drawText(80,650 + 8,line_name);
	//    painter.drawText(100, 30, title);

	// draw text of y
	//    font.setPixelSize(15);
	//    painter.drawText(55, 547, QString::number(min));
	//    qreal y_temp = (max - min) > gap ? max : (min + gap) ;
	//    painter.drawText(55, 147, QString::number(y_temp)); // (max - min) > gap ? max : (min + gap)
	//    painter.drawText(55, 347, QString::number((y_temp-min) / 2));

	// draw text of x
	//    for (int i=0; i<line_xname.size(); i++)
	//    {
	//        painter.drawText(40 + 50*i, 570, line_xname.at(i));
	//    }

	//    int Counter=0;
	//    while(Counter < (xList.size()-1))
	//    {
	//        painter.setPen(pen);
	//        painter.drawLine(xList.value(Counter)*25+50,550-yDrawList.value(Counter)/(gap)*400,
	//                         xList.value(Counter+1)*25+50,550-yDrawList.value(Counter+1)/(gap)*400);
	//        Counter++;
	//    }
	painter.setPen(pen);
	switch (xNum)
	{
	case 0:
	case 1:
		break;
	case 2:
	case 3:
	case 4:
	case 5:
	case 6:
	case 7:
		for (int i=0; i<line_y.size()-1; i++)
		{
			//            painter.setPen(pen);
			painter.drawLine(i*100+50, 550 - line_y.at(i)/(gap)*400,
					(i+1)*100+50, 550 - line_y.at(i+1)/(gap)*400);
		}
		break;
	case 12:
	case 13:
		for (int i=0; i<line_y.size()-1; i++)
		{
			//            painter.setPen(pen);
			painter.drawLine((i)*50+50, 550 - line_y.at(i)/(gap)*400,
					(i+1)*50+50, 550 - line_y.at(i+1)/(gap)*400);
		}
		break;
	case 30:
	case 31:
		for (int i=0; i<line_y.size()-1; i++)
		{
			//            painter.setPen(pen);
			painter.drawLine((i)*50/3+50, 550 - line_y.at(i)/(gap)*400,
					(i+1)*50/3+50, 550 - line_y.at(i+1)/(gap)*400);
		}
		break;
	default:
		for (int i=0; i<line_y.size()-1; i++)
		{
			//            painter.setPen(pen);
			painter.drawLine((i)*25+50, 550 - line_y.at(i)/(gap)*400,
					(i+1)*25+50, 550 - line_y.at(i+1)/(gap)*400);
		}
		break;
	}

	QString path = QDir::tempPath() + "/linechart" + QDateTime::currentDateTime().toString() + ".png";
//	QDir dir(QDir::homePath());
//	qDebug() << "remove file : " << dir.remove("linechart" + title + ".png");
	bool save = pixmap.save(path, "PNG");
	if (save)
	{
		emit drawChartFinish(path);
		this->ChartPath = path ;
	}
	else
		emit drawChartError();
}

void DrawChart::drawPieChart(QString title)
{
	QString id("white");
	QImage pixmap(700,600,QImage::Format_ARGB32_Premultiplied);
	pixmap.fill(QColor(id).rgba());

	// write the color name
	QFont font;
	font.setPixelSize(24);
	QPainter painter(&pixmap);
	painter.setRenderHint(QPainter::Antialiasing);
	painter.setFont(font);
	//    QStaticText st(title);
	//    painter.drawText(100, 30, title);
	Nightcharts PieChart;
	PieChart.setType(Nightcharts::Dpie);//{Histogramm , Pie, Dpie};
	PieChart.setLegendType(Nightcharts::Vertical);//{Round,Vertical}
	PieChart.setCords(50,50,600,480);
	//    PieChart.addPiece("Item1",QColor(200,10,50),34);
	//    PieChart.addPiece("Item2",Qt::green,27);
	//    PieChart.addPiece("Item3",Qt::cyan,14);
	//    PieChart.addPiece("Item4",Qt::yellow,7);
	//    PieChart.addPiece("Item5",Qt::blue,4);
	for (int i=0; i<pie_name.size(); i++)
	{
		PieChart.addPiece(pie_name.at(i),QColor(pie_color.at(i)),pie_percent.at(i));
	}
	PieChart.draw(&painter);
	//    PieChart.drawLegend(&painter);
	QString path = QDir::tempPath() + "/piechart" + QDateTime::currentDateTime().toString() + ".png";
//	QDir dir(QDir::tempPath());
//	qDebug() << "remove file : " << dir.remove("piechart" + title + ".png");
	bool save = pixmap.save(path, "PNG");
	if (save)
	{
		emit drawChartFinish(path);
		this->ChartPath = path ;
	}
	else
		emit drawChartError();
}

void DrawChart::inputLineChart(qreal yNum)
{
	line_y.append(yNum);
}

void DrawChart::inputLineXName(QString xName)
{
	line_xname.append(xName);
}

void DrawChart::inputPieChart(QString name, QString color, float percentage)
{
	pie_name.append(name);
	pie_color.append(color);
	pie_percent.append(percentage);
}

void DrawChart::inputClear()
{
	line_y.clear();
	line_xname.clear();
	pie_name.clear();
	pie_color.clear();
	pie_percent.clear();
}

void DrawChart::saveChart()
{
	if (QFile::copy(this->ChartPath, QDir::currentPath() + "/shared/photos/chart.png"))
		emit savedchart(true);
	else
		emit savedchart(false);
}

qreal DrawChart::findMax()
{
	QList<qreal> yList = this->line_y;
	qreal max = yList.at(0);
	for (int i=1; i<yList.size(); i++)
	{
		if (yList.at(i) > max)
		{
			max = yList.at(i);
		}
	}
	return max;
}

qreal DrawChart::findMin()
{
	QList<qreal> yList = this->line_y;
	qreal min = yList.at(0);
	for (int i=1; i<yList.size(); i++)
	{
		if (yList.at(i) < min)
		{
			min = yList.at(i);
		}
	}
	return min;
}
